﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;

namespace AimGameLibrary.GameObject
{
    public enum DirectionInCollision
    { 
        Top,
        Bottom,
        Left,
        Right,
        None
    }

    //class DirectionLength
    //{
    //    public float Leangth;
    //    public DirectionInCollision direction;

    //    public DirectionLength(float Leangth,DirectionInCollision direction)
    //    {
    //        this.Leangth = Leangth;
    //        this.direction = direction;
    //    }
    //}

    public class GameCollision : BaseGameObject
    {
        bool ActiveTop = true;
        bool AcTiveBottom = true;
        bool ActiveLeft = true;
        bool ActiveRight = true;
        Boolean myboolean = false;
        public Boolean MybooleaN { get { return myboolean; } set { myboolean = value; } }

        public override void LoadData(Identification getdata, int IndexData, NodeData node)
        {
            Listnameparent.Add(Script.GetName(node.Data, 2, 0));
            Listnameparent.Add(Script.GetName(node.Data, 2, 1));
        }

        public override void UpdateTime(TimeSpan timespan)
        {
            base.UpdateTime(timespan);

            CollisionUpdate();
        }

        public override void UpdateGameTime(GameTime gametime)
        {
            base.UpdateGameTime(gametime);

            CollisionUpdate();
        }

        void CollisionUpdate()
        {
            if (Listparent.Count == 2)
            {
                if (Listparent[0].GetType() == typeof(GameBody) && Listparent[1].GetType() == typeof(GameBody))
                {
                    GameBody tempbody1 = (GameBody)Listparent[0];
                    GameBody tempbody2 = (GameBody)Listparent[1];
                    Rectangle rectangle1 = new Rectangle((int)tempbody1.MybodY.X, (int)tempbody1.MybodY.Y, (int)tempbody1.MybodY.MyWidth, (int)tempbody1.MybodY.MyHight);
                    Rectangle rectangle2 = new Rectangle((int)tempbody2.MybodY.X, (int)tempbody2.MybodY.Y, (int)tempbody2.MybodY.MyWidth, (int)tempbody2.MybodY.MyHight);

                    switch (DirectionCollision(rectangle1, rectangle2))
                    {
                        case DirectionInCollision.Top:
                            if (ActiveTop)
                            {
                                tempbody1.MybodY = new Body(rectangle1.X, rectangle2.Bottom, rectangle1.Width, rectangle1.Height, tempbody1.MybodY.MyForce);
                                //tempbody1.myDirectioninCollision = DirectionInCollision.Top;
                                this.MybooleaN = true;
                            }
                            break;
                        case DirectionInCollision.Bottom:
                            if (AcTiveBottom)
                            {
                                tempbody1.MybodY = new Body(rectangle1.X, rectangle2.Top - rectangle1.Height , rectangle1.Width, rectangle1.Height, 0.0f);
                                //tempbody1.myDirectioninCollision = DirectionInCollision.Bottom;
                                this.MybooleaN = true;
                            }
                            break;
                        case DirectionInCollision.Right:
                            if (ActiveLeft)
                            {
                                tempbody1.MybodY = new Body(rectangle2.Right, rectangle1.Y, rectangle1.Width, rectangle1.Height, tempbody1.MybodY.MyForce);
                                //tempbody1.myDirectioninCollision = DirectionInCollision.Left;
                                this.MybooleaN = true;
                            }
                            break;
                        case DirectionInCollision.Left:
                            if (ActiveRight)
                            {
                                tempbody1.MybodY = new Body(rectangle2.Left - rectangle1.Width, rectangle1.Y, rectangle1.Width, rectangle1.Height, tempbody1.MybodY.MyForce);
                                //tempbody1.myDirectioninCollision = DirectionInCollision.Right;
                                this.MybooleaN = true;
                            }
                            break;
                        case DirectionInCollision.None:
                            //tempbody1.myDirectioninCollision = DirectionInCollision.None;
                            this.MybooleaN = false;
                            break;
                    }
                }
            }

        }

        DirectionInCollision DirectionCollision(Rectangle target, Rectangle objecttive)
        {
            DirectionInCollision direction = DirectionInCollision.None;

            List<float> Listfloat = new List<float>();
            if (target.Intersects(objecttive))
            {
                //Result Value Intersects in Direction
                Listfloat.Add((float)objecttive.Bottom - (float)target.Top); // Top
                Listfloat.Add((float)target.Bottom - (float)objecttive.Top); // Bottom
                
                Listfloat.Add((float)target.Right - (float)objecttive.Left); // Left!!
                Listfloat.Add((float)objecttive.Right - (float)target.Left); // Right!!

                // Conselp Find Max Lenght in Direction to Collision
                return direction = (DirectionInCollision)Listfloat.IndexOf(Listfloat.Min());

            }
            else return DirectionInCollision.None;
        }
    }
}
